// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/sighting/api/SightingDeviceApi'

import OneClass from '@api/sighting/model/SightingDeviceDetailReq'
import SubmitClass from '@api/sighting/model/SightingDeviceScreenShotReq'
import EditClass from '@api/sighting/model/SightingDeviceSettingReq'
import DelClass from '@api/sighting/model/SightingDeviceRebootReq'
import PassClass from '@api/sighting/model/SightingDeviceOpenScreenReq'
import RejClass from '@api/sighting/model/SightingDeviceCloseScreenReq'
import BaseClass from '@api/sighting/model/SightingDeviceDto'

const CLIENT = CORE.grpcCLIENT(Client)
const BASE = [BaseClass, 'sightingDeviceDto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询单项') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'sightingDeviceDetail', DTO)
    return resp.sightingDeviceDto || {}
  },
  transOne(obj, { state: { INIT_ONE } }) {
    const volumeList = JSON.parse(obj.volumeSetting || '[]')
    const screenList = JSON.parse(obj.terminalScreen || '[]')
    const powerList = JSON.parse(obj.terminalPower || '[]')
    obj.volumeList = INIT_ONE.volumeList.map((item) => trans(item, volumeList, 'openscreen', 'closescreen'))
    obj.screenList = INIT_ONE.screenList.map((item) => trans(item, screenList, 'openscreen', 'closescreen'))
    obj.powerList = INIT_ONE.powerList.map((item) => trans(item, powerList, 'poweron', 'poweroff'))
    return obj
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '保存') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'sightingDeviceSetting', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  async submit({ deviceDto: { mac } }, TIP = '截图') {
    const DTO = CORE.grpcDTO(SubmitClass, { mac })
    const resp = await CORE.grpc(TIP, CLIENT, 'sightingDeviceScreenshot', DTO)
    window.open(CORE.image(resp.msg))
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ deviceDto: { thirdCode } }, TIP = '重启') {
    const DTO = CORE.grpcDTO(DelClass, { thirdDeviceId: thirdCode })
    await CORE.grpc(TIP, CLIENT, 'sightingDeviceReboot', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 通过 —————————————————————————————— //

  async pass({ deviceDto: { thirdCode } }, TIP = '开屏') {
    const DTO = CORE.grpcDTO(PassClass, { thirdDeviceId: thirdCode })
    await CORE.grpc(TIP, CLIENT, 'sightingDeviceOpenScreen', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 拒绝 —————————————————————————————— //

  async rej({ deviceDto: { thirdCode } }, TIP = '关屏') {
    const DTO = CORE.grpcDTO(RejClass, { thirdDeviceId: thirdCode })
    await CORE.grpc(TIP, CLIENT, 'sightingDeviceCloseScreen', DTO)
    return CORE.grpcOK(TIP)
  },
}

// ============================== 内部方法 ============================== //

const RANGE = { beginHour: '00', beginMinute: '00', endHour: '23', endMinute: '59' }

const trans = (obj, arr, begin, end) => {
  const matched = arr.find((item) => item.day === obj.day)
  if (!matched) return { ...obj }
  return {
    ...matched,
    [begin]: matched[begin] || `${RANGE.beginHour}:${RANGE.beginMinute}`,
    [end]: matched[begin] || `${RANGE.endHour}:${RANGE.endMinute}`,
    beginHour: matched[begin] ? matched[begin].slice(0, 2) : RANGE.beginHour,
    beginMinute: matched[begin] ? matched[begin].slice(-2) : RANGE.beginMinute,
    endHour: matched[end] ? matched[end].slice(0, 2) : RANGE.endHour,
    endMinute: matched[end] ? matched[end].slice(-2) : RANGE.endMinute,
  }
}
